/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mabi.frontend.appbuilder;

import ClassMonger.ZipClassLoader;
import com.mabi.frontend.FrontEndApp;
import com.mabi.frontend.mock.Mock;
import com.mabi.modular.admin.modules.IModuleDefinition;
import com.mabi.modular.runtime.module.AbstractMabiModule;
import com.mabi.modular.runtime.module.contributions.views.View;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.NamingException;

/**
 *
 * @author User
 */
public class ModuleManager {

    private static void testClassLoading() throws MalformedURLException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException, IOException {
        //TODO: IMPLEMENT

        String jarpath = "/Users/User/Desktop/Mabi1.1/buscadorEmpleados/dist/buscadorEmpleados.jar";
        String nc = "com.test.buscadorempleados.ModuloBuscador";
        File f = new File(jarpath);
        
        
        ModuleLoader ml = new ModuleLoader();
        try {
            List<IModuleDefinition> loadModules = ml.loadModules();
        } catch (NamingException ex) {
            Logger.getLogger(ModuleManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    private FrontEndApp frontEndApp;
    private static ModuleManager instance;
    private List<AbstractMabiModule> installedModules;

    public static ModuleManager getDefault(FrontEndApp app) {
        if (null == instance) {
            instance = new ModuleManager(app);
        }
        return instance;
    }

    private ModuleManager(FrontEndApp app) {
        frontEndApp = app;
    }

    /**
     * Installs the modules into the given application
     */
    public void installModules() {
        //Get loaded modules from repository
        List<AbstractMabiModule> installedModules = null;
        installedModules = getInstalledModules();

        //Subscribe module in module manager
        for (AbstractMabiModule module : installedModules) {
            subscribeModule(module);
        }

        for (AbstractMabiModule module : this.installedModules) {

            //Let's install the module views
            installViews(module);

            //Let's install the menu items

            //Let's install the commands
        }
    }

    private static List<AbstractMabiModule> getInstalledModules() {
        try {
            testClassLoading();

            //try {

            //Runtime.getRuntime().load("/Users/User/Desktop/Mabi1.1/buscadorEmpleados/dist/buscadorEmpleados.jar");

            //        try {
            //            //Class<?> loadClass = ModuleManager.class.getClassLoader().loadClass("com.test.buscadorempleados.ModuloBuscador");
            //        } catch (ClassNotFoundException ex) {
            //            Logger.getLogger(ModuleManager.class.getName()).log(Level.SEVERE, null, ex);
            //        }

            //            JarFile jf = new JarFile(f);
            //            Enumeration<JarEntry> entries = jf.entries();
            //            ClassLoader classLoader = ModuleManager.class.getClassLoader();
            //            while (entries.hasMoreElements()) {
            //                JarEntry jarEntry = entries.nextElement();
            //                String name = jarEntry.getName();
            //               
            //                System.out.println("NAME : " + name);
            //                
            //                if(name.contains(".class")){
            //                    System.load(name);
            //                    
            //                }
            //               
            //            }
            //        } catch (IOException ex) {
            //            Logger.getLogger(ModuleManager.class.getName()).log(Level.SEVERE, null, ex);
            //        }
        } catch (MalformedURLException ex) {
            Logger.getLogger(ModuleManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ModuleManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(ModuleManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(ModuleManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(ModuleManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(ModuleManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(ModuleManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ModuleManager.class.getName()).log(Level.SEVERE, null, ex);
        }
                  


        return Mock.getInstalledModules();
    }

    /**
     * Installs autoload modules views
     * @param module
     */
    private void installViews(AbstractMabiModule module) {
        List<View> views = module.getViews();
        if (views == null) {
            return;
        }
        if (views.size() > 0) {
            for (View view : views) {
                view.setModuleName(module.getName());
                if (view.isAutoLoad()) {
                    try {
                        frontEndApp.addView(view);
                    } catch (Exception ex) {
                        Logger.getLogger(ModuleManager.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }
    }

    private void subscribeModule(AbstractMabiModule module) {
        if (this.installedModules == null) {
            this.installedModules = new ArrayList();
        }
        module.setUpContext(frontEndApp.getAppContext(), frontEndApp);
        this.installedModules.add(module);
    }
}
